package com.example.servicegateway.utils;

import com.alibaba.fastjson.JSON;
import com.example.servicegateway.constants.AuthConstant;
import com.example.servicegateway.entity.TokenInfo;
import com.nimbusds.jose.JWSObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;

@Slf4j
public class TokenUtils {

    public static TokenInfo wrapper(String token) {
        try {
            String realToken = StringUtils.replaceIgnoreCase(token, AuthConstant.JWT_TOKEN_PREFIX, StringUtils.EMPTY);
            JWSObject jwsObject = JWSObject.parse(realToken);
            String payload = jwsObject.getPayload().toString();
            return JSON.parseObject(payload, TokenInfo.class);
        } catch (ParseException e) {
            log.error("jws转换异常", e);
            throw new RuntimeException(e);
        }
    }
}
